package com.example.demo.servlet;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "Filter_first")
public class Filter_first implements Filter {
    public void init(FilterConfig config) throws ServletException {
        System.out.println("启用");
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest httpRequest=(HttpServletRequest)request;
        String username=httpRequest.getParameter("username");
        HttpSession session=httpRequest.getSession();
        String url=httpRequest.getRequestURI();
        System.out.println(url);
        if (session.getAttribute("username")!=null||username!=null||url.equals("/login")||url.equals("/static/css/style.css")||url.equals("/favicon.ico")||url.equals("/static/1.jpg")||url.equals("/sign")){
            chain.doFilter(request, response);
            System.out.println("开始");
        }else{
            HttpServletResponse httpResponse=(HttpServletResponse) response;
            httpResponse.sendRedirect("/login");
            System.out.println("继续");
        }

    }
}
